View Javadoc

1   package org.robsite.extension.rss;
2   
3   import java.awt.Component;
4   
5   import java.text.MessageFormat;
6   
7   import java.util.ResourceBundle;
8   
9   import javax.swing.AbstractButton;
10  import javax.swing.JLabel;
11  
12  import oracle.bali.share.nls.StringUtils;
13  
14  /***
15   * Resource helper.
16   * 
17   * @author brian_duff@sourceforge.net
18   * @version $Revision: 1.1.1.1 $
19   */
20  final class ResHelper 
21  {
22    private ResourceBundle _bundle = ResourceBundle.getBundle(
23      "org.robsite.extension.rss.Resource"
24    );
25    private String _baseKey;
26    
27    ResHelper( String baseKey )
28    {
29      if ( baseKey.endsWith( "." ) )
30      {
31        _baseKey = baseKey;
32      }
33      else
34      {
35        _baseKey = baseKey + ".";
36      }
37    }
38    
39    /***
40     * Get the value of a resource.
41     * 
42     * @param key the resource key to look up.
43     * @return the value of a resource.
44     */
45    String getRes( String key )
46    {
47      return _bundle.getString( _baseKey + key );
48    }
49    
50    /***
51     * Get the value of a resource, substituting the specified string into the
52     * result.
53     * 
54     * @param key a resource key to look up.
55     * @param subst a string to substitute into the resource value.
56     * @return the translated string.
57     */
58    String getRes( String key, String subst )
59    {
60      return MessageFormat.format( 
61        getRes( key ),
62        new String[] { subst }
63      );
64    }
65    
66    /***
67     * Utility that sets the text of a label, including any mnemonic indicated
68     * by an ampersand in the value of the resource. Also associates the label
69     * with a component for accessibility.
70     * 
71     * @param key the resource key to use for the text of the label.
72     * @param label the label to set properties of.
73     * @param labelForComponent a component to associate with the label. May be
74     *    null.
75     */
76    void resLabel( String key, JLabel label, Component labelForComponent )
77    {
78      String labelText = getRes( key );
79      int mnemonicCode = StringUtils.getMnemonicKeyCode( labelText );
80     // int mnemonicIndex = StringUtils.getMnemonicIndex( labelText );
81      if ( mnemonicCode != -1 )
82      {
83        label.setDisplayedMnemonic( mnemonicCode );
84        //label.setDisplayedMnemonicIndex( mnemonicIndex );
85      }
86      label.setText( StringUtils.stripMnemonic( labelText ) );
87      if ( labelForComponent != null )
88      {
89        label.setLabelFor( labelForComponent );
90      }
91    }
92    
93    
94    /***
95     * Utility that sets the text of a button, including any mnemonic indicated
96     * by an ampersand in the value of the resource.
97     * 
98     * @param key the resource key to use for the text of the button.
99     * @param button the button to set properties of.
100    */  
101   void resButton( String key, AbstractButton button )
102   {
103     String buttonText = getRes( key );
104     int mnemonicCode = StringUtils.getMnemonicKeyCode( buttonText );
105     //int mnemonicIndex = StringUtils.getMnemonicIndex( buttonText );
106     if ( mnemonicCode != -1 )
107     {
108       button.setMnemonic( mnemonicCode );
109      // button.setDisplayedMnemonicIndex( mnemonicIndex );
110     }
111     button.setText( StringUtils.stripMnemonic( buttonText ) );
112   }  
113 }